package com.base.caselib.presenter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Base64;

import com.base.caselib.bean.CertCardBean;
import com.base.caselib.bean.DriverBean;
import com.base.caselib.bean.DrivingLicenseBean;
import com.base.caselib.view.CameraView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/2/2.
 */

public class CameraPresenter extends BasePresenter<CameraView> {

    public CameraPresenter(CameraView view, Activity activity) {
        super(view, activity);
    }
    //身份证信息
    public void getCret(String str, final String path){
        mView.showPb_mes("正在初始化");
        Map<String,String> params = new HashMap<>();
        params.put("id_card_side","front");
        params.put("detect_direction","true");
        params.put("image", str);
        Disposable disposable = RealNetWorkUtil.postContentForCard()
                .method("recognition/idcard")
                .content(params)
                .build().enqueueForCard(CertCardBean.class, new CallBackImpl<CertCardBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(CertCardBean bean) {
                        mView.hidePb_mes();
                        bean.setPath(path);
                        RxBus2.getInstance().post(bean);
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    //驾驶证
    public void getDrivLisence(String str, final String path){
        mView.showPb_mes("正在初始化");
        Map<String,String> params = new HashMap<>();
        params.put("vehicle_license_side","front");
        params.put("detect_direction","true");
        params.put("image", str);
        Disposable disposable = RealNetWorkUtil.postContentForCard()
                .method("recognition/vehicleLicense")
                .content(params)
                .build().enqueueForCard(DrivingLicenseBean.class, new CallBackImpl<DrivingLicenseBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(DrivingLicenseBean objects) {
                        mView.hidePb_mes();
                        objects.setPath(path);
                        RxBus2.getInstance().post(objects);
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    //驾驶证
    public void getDriver(String str, final String path){
        mView.showPb_mes("正在初始化");
        Map<String,String> params = new HashMap<>();
        params.put("image", str);
        Disposable disposable = RealNetWorkUtil.postContentForCard()
                .method("recognition/drivingLicense")
                .content(params)
                .build().enqueueForCard(DriverBean.class, new CallBackImpl<DriverBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(DriverBean objects) {
                        mView.hidePb_mes();
                        objects.setPath(path);
                        RxBus2.getInstance().post(objects);
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    /**
     * bitmap转为base64
     * @param bitmap
     * @return
     */
    public String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
